package com.example.speeddating.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginAdmin;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.*;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.service.TagsService;
import com.example.speeddating.service.UserTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:34
 */
@Slf4j
@RestController
public class TagsController {

    @Autowired
    private TagsService tagsService;

    @Autowired
    private UserTagService userTagService;

    /**
     * 所有标签
     * @return
     */
    @GetMapping("/tags/all")
    public BaseResponse  getTags() {
        return RespGenerator.returnOK(tagsService.all());
    }

    /**
     * 管理员-所有标签
     * @return
     */
    @TokenCheck(roles = "admin")
    @GetMapping("/admin/tags/all")
    public BaseResponse  getTags(@LoginAdmin Admin admin) {
        List all = tagsService.all();
        Page<List> tagTypePage = new Page<>();
        tagTypePage.setPages(1);
        tagTypePage.setTotal(all.size());
        tagTypePage.setRecords(all);
        return RespGenerator.returnOK(tagTypePage);
    }


    /**
     * 得到所有标签
     * @return
     */
    @GetMapping("/tags/all_user")
    @TokenCheck(roles = "user")
    public BaseResponse  getTagsByUser(@LoginUser TokenEntity tokenEntity) {
        return RespGenerator.returnOK(tagsService.all_user(tokenEntity.getId()));
    }

    /**
     * 用户标签
     */
    @GetMapping("/user/tags")
    @TokenCheck(roles = "user")
    public BaseResponse getUserTags(@LoginUser TokenEntity tokenEntity) {
        return RespGenerator.returnOK(tagsService.selectUserTagsByUserId(tokenEntity.getId()));
    }
    /**
     * 得到用户所有标签
     * @return
     */
    @GetMapping("/user/tags/{id}")
//    @TokenCheck(roles = "user")
    public BaseResponse  getUserTags(@PathVariable Long id) {
        return RespGenerator.returnOK(tagsService.selectUserTagsByUserId(id));
    }

    /**
     * 用户选中标签 根据当前的token 来判断用户
     * 传入标签的id
     */
    @PostMapping("/user/selectTags/{id}")
    @TokenCheck(roles = "user")
    public BaseResponse addUserTags(@LoginUser TokenEntity tokenEntity, @PathVariable Integer id) {
        UserTag userTag = new UserTag();
        userTag.setUserId(tokenEntity.getId());
        userTag.setTagId(id);
        boolean save = userTagService.save(userTag);
        if (save)return RespGenerator.returnOK("添加成功");
        return RespGenerator.returnOK("添加失败");
    }

    /**
     * 用户取消选中标签
     */
    @PostMapping("/user/cancleTags/{id}")
    @TokenCheck(roles = "user")
    public BaseResponse cancleUserTags(@LoginUser TokenEntity tokenEntity, @PathVariable Integer id) {
        QueryWrapper<UserTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",tokenEntity.getId());
        queryWrapper.eq("tag_id",id);
        boolean save = userTagService.remove(queryWrapper);
        if (save)return RespGenerator.returnOK("取消成功");
        return RespGenerator.returnOK("取消失败");
    }

    /**
     * 管理员添加标签
     */
    @TokenCheck(roles = "admin")
    @PostMapping("/admin/tag/add")
    public BaseResponse addTags(@LoginAdmin Admin admin,@RequestBody Tags tags){
        log.info(tags.getTagName(),tags.getTypeId());
        tagsService.insertTags(tags);
        return RespGenerator.returnOK("添加成功");
    }
    /**
     * 管理员删除标签
     */
    @TokenCheck(roles = "admin")
    @DeleteMapping("/admin/tag/del/{id}")
    public BaseResponse delTags(@LoginAdmin Admin admin,@PathVariable("id") Integer id){
        //得到标签名 和 隶属于哪个父标签下
        tagsService.deleteTags(id);
        return RespGenerator.returnOK("删除成功");
    }

    /**
     * 管理员修改标签
     * @param tags
     * @return
     */
    @PutMapping("/admin/updateTag")
    public BaseResponse updateTag(@RequestBody Tags tags){
        tagsService.updateTags(tags);
        return RespGenerator.returnOK("更新成功");
    }

}
